home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / cad / lasi512b.zip / TILT.C < prev    next >
C/C++ Source or Header  |  1996-04-04  |  23KB  |  1,025 lines

  1.  
  2. //This is the source code for the TILT.EXE utility program. It is included
  3. //as an example of how to write simple programs that work with LASI cells
  4. //in TLC format. Most of the functions are generic and may be used in other
  5. //programs. Compile with Microsoft C/C++ (or other compiler) in small model.
  6.  
  7. //*****************
  8. // TLC Tilter v5.1
  9. // by d.e.boyce
  10. //*****************
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <graph.h>
  15. #include <conio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19.  
  20.  
  21. #define PSS     512
  22. #define IL      32767
  23. #define LIL     8388607
  24. #define PIX2    6.2831853
  25. #define PIF     3.1415926
  26. #define PID2    1.5707963
  27. #define DR      57.29577951
  28. #define TLCLIM  100
  29.  
  30. //function prototypes
  31. int ropenTLC(char *);
  32. int wopenTLC(char *);
  33. int read4eqCode(void);
  34. int read4Hdr(void);
  35. int readC(void);
  36. int readB(void);
  37. int readP(void);
  38. int readT(void);
  39. void writeH(void);
  40. void writeC(void);
  41. void writeB(void);
  42. void writeP(void);
  43. void writeT(void);
  44. void makeH(void);
  45. void makeC(void);
  46. void makeP(void);
  47. void makeB(void);
  48. void makeT(void);
  49. void path2poly(void);
  50. int pathSeg(long,int,int,int);
  51. int makeTLCxy(void);
  52. int abrtCk(void);
  53. long lround(double);
  54. void Tilt(void);
  55. void tiltX(double,double);
  56. void tiltY(double,double);
  57. void tiltZ(double,double);
  58. char *chop(char *,int);
  59. char *tiltname(char *);
  60. int dsign(double);
  61. void HELP(void);
  62.  
  63.  
  64. //global variables
  65. char header[]="\nLASI TLC Tilter  Version 5.1\n\n";
  66.  
  67. //command line variables
  68. double ang1,ang2,ang3,lspace,cspace;
  69. double cos1,cos2,cos3,sin1,sin2,sin3;
  70. char axis1,axis2,axis3;
  71.  
  72. //coordinate variables
  73. long nx,ny,nz,p1,p2,p3,q1,q2,q3;
  74. int npp;
  75.  
  76. //poly coord array
  77. long px[PSS],py[PSS];
  78.  
  79. //TLC file structs
  80. FILE *rTLCfile,*wTLCfile;
  81.  
  82. //TLC record structs
  83. struct tlchdr
  84. {
  85.     char name[10];
  86.     char lv[6];
  87.     char tv[6];
  88.     double scale;
  89.     char punit[6];
  90.     char date[9];
  91.     char time[9];
  92.     int rnk;
  93.     long l;
  94.     long b;
  95.     long r;
  96.     long t;
  97.     int bn;
  98.     int pn;
  99.     unsigned vn;
  100.     int cn;
  101. } TLCh={"","5.1","5.1",20,"um",1,0,0,10,10,0,0,0,0};
  102.  
  103.  
  104. struct tlccel
  105. {
  106.     char name[9];
  107.     int o;
  108.     long x;
  109.     long y;
  110.     int r;
  111. } TLCc;
  112.  
  113. struct tlcbox
  114. {
  115.     int lyr;
  116.     long l;
  117.     long b;
  118.     long r;
  119.     long t;
  120. } TLCb;
  121.  
  122. struct tlcpath
  123. {
  124.     int lyr;
  125.     long w;
  126.     int nv;
  127.     long x[PSS];
  128.     long y[PSS];
  129. } TLCp;
  130.  
  131. struct tlctext
  132. {
  133.     int lyr;
  134.     long siz;
  135.     int nv;
  136.     int o;
  137.     long x;
  138.     long y;
  139.     char txt[41];
  140. } TLCt;
  141.  
  142.  
  143. //*************
  144. //start of main
  145. //*************
  146.  
  147. main(int argc,char *argv[])
  148. {
  149.     int cp=0;
  150.  
  151.     _clearscreen(_GCLEARSCREEN);
  152.  
  153.     printf("%s",header);
  154.  
  155.     if(argc<6){
  156.         HELP();
  157.         return 0;
  158.     }
  159.  
  160.     //command line param
  161.     ang1=atof(argv[1]);
  162.     axis1=tolower(argv[1][strlen(argv[1])-1]);
  163.     ang2=atof(argv[2]);
  164.     axis2=tolower(argv[2][strlen(argv[2])-1]);
  165.     ang3=atof(argv[3]);
  166.     axis3=tolower(argv[3][strlen(argv[3])-1]);
  167.  
  168.     lspace=atof(argv[4]);
  169.     cspace=atof(argv[5]);
  170.  
  171.     //angle cos and sin
  172.     cos1=cos(ang1/DR);
  173.     sin1=sin(ang1/DR);
  174.     cos2=cos(ang2/DR);
  175.     sin2=sin(ang2/DR);
  176.     cos3=cos(ang3/DR);
  177.     sin3=sin(ang3/DR);
  178.  
  179.     printf("Tilting %gdeg in %c  %gdeg in %c  %gdeg in %c\n\n",
  180.     ang1,axis1,ang2,axis2,ang3,axis3);
  181.  
  182.     //TLC file tilter loop
  183.     for(cp=6;cp<argc;cp++){
  184.  
  185.         if(!ropenTLC(argv[cp]))
  186.             continue;
  187.         if(!wopenTLC(argv[cp]))
  188.             continue;
  189.  
  190.         read4Hdr();
  191.         //init counts to 0
  192.         TLCh.bn=TLCh.pn=TLCh.vn=TLCh.cn=0;
  193.         rewind(rTLCfile);
  194.  
  195.         read4eqCode();
  196.         makeH();
  197.  
  198.         printf("  %s.TLC File Generated!\n",tiltname(argv[cp]));
  199.         _fcloseall();
  200.         if(abrtCk()) break;
  201.     }
  202.     return 0;
  203. }
  204.  
  205. //******************
  206. //start of functions
  207. //******************
  208.  
  209. //read TLC header info
  210. int read4Hdr(void)
  211. {
  212.     static char buf[TLCLIM];
  213.     char *s;
  214.  
  215.     buf[0]=0;
  216.     while(!feof(rTLCfile)){
  217.         if(!strncmp(fgets(buf,TLCLIM,rTLCfile),"=H",2)){
  218.             fgets(buf,TLCLIM,rTLCfile);
  219.             strcpy(TLCh.name,chop(buf,8));
  220.  
  221.             fgets(buf,TLCLIM,rTLCfile);
  222.             strcpy(TLCh.lv,chop(buf,5));
  223.  
  224.             fgets(buf,TLCLIM,rTLCfile);
  225.             strcpy(TLCh.tv,chop(buf,5));
  226.  
  227.             fgets(buf,TLCLIM,rTLCfile);
  228.             TLCh.scale=atof(buf);
  229.  
  230.             fgets(buf,TLCLIM,rTLCfile);
  231.             strcpy(TLCh.punit,chop(buf,6));
  232.  
  233.             fgets(buf,TLCLIM,rTLCfile);
  234.             strcpy(TLCh.date,chop(buf,13));
  235.  
  236.             fgets(buf,TLCLIM,rTLCfile);
  237.             strcpy(TLCh.time,chop(buf,10));
  238.  
  239.             fgets(buf,TLCLIM,rTLCfile); //outline
  240.             chop(buf,36);
  241.             if((s=strtok(buf," "))==NULL) return 0;
  242.             TLCh.rnk=atoi(s);
  243.             if((s=strtok(NULL," "))==NULL) return 0;
  244.             TLCh.l=atol(s);
  245.             if((s=strtok(NULL," "))==NULL) return 0;
  246.             TLCh.b=atol(s);
  247.             if((s=strtok(NULL," "))==NULL) return 0;
  248.             TLCh.r=atol(s);
  249.             if((s=strtok(NULL," "))==NULL) return 0;
  250.             TLCh.t=atol(s);
  251.  
  252.             fgets(buf,TLCLIM,rTLCfile); //count
  253.             chop(buf,30);
  254.             if((s=strtok(buf," "))==NULL) return 0;
  255.             TLCh.bn=atoi(s);
  256.             if((s=strtok(NULL," "))==NULL) return 0;
  257.             TLCh.pn=atoi(s);
  258.             if((s=strtok(NULL," "))==NULL) return 0;
  259.             TLCh.vn=atoi(s);
  260.             if((s=strtok(NULL," "))==NULL) return 0;
  261.             TLCh.cn=atoi(s);
  262.         }
  263.     }
  264.     return 1;
  265. }
  266.  
  267. //read for record types then write new records
  268. int read4eqCode(void)
  269. {
  270.     static char buf[TLCLIM];
  271.  
  272.     buf[0]=0;
  273.  
  274.     while(!feof(rTLCfile)){
  275.  
  276.         fgets(buf,TLCLIM,rTLCfile);
  277.  
  278.         if(feof(rTLCfile))   //eof check
  279.             break;
  280.  
  281.         if(!strncmp(buf,"=C",2)){
  282.             if(readC())
  283.                 makeC();
  284.             else
  285.                 printf("  Cell Record Read Error!\n");
  286.             continue;
  287.         }
  288.         if(!strncmp(buf,"=B",2)){
  289.             if(readB())
  290.                 makeB();
  291.             else
  292.                 printf("  Box Record Read Error!\n");
  293.             continue;
  294.         }
  295.         if(!strncmp(buf,"=P",2)){
  296.             if(readP())
  297.                 makeP();
  298.             else
  299.                 printf("  Path Record Read Error!\n");
  300.             continue;
  301.         }
  302.         if(!strncmp(buf,"=T",2)){
  303.             if(readT())
  304.                 makeT();
  305.             else
  306.                 printf("  Text Record Read Error!\n");
  307.             continue;
  308.         }
  309.     }
  310.     return 1;
  311. }
  312.  
  313. void makeH(void)
  314. {
  315.     char *p;
  316.  
  317.     p=tiltname(TLCh.name);
  318.     strcpy(TLCh.name,p);
  319.     nx=TLCh.l;ny=TLCh.b;nz=0;
  320.     Tilt();
  321.     TLCh.l=nx;TLCh.b=ny;
  322.     nx=TLCh.r;ny=TLCh.t;nz=0;
  323.     Tilt();
  324.     TLCh.r=nx;TLCh.t=ny;
  325.     writeH();
  326. }
  327.  
  328. void makeC(void)
  329. {
  330.     char *p;
  331.  
  332.     //change name and tilt ref point
  333.     p=tiltname(TLCc.name);
  334.     nz=(long)((TLCh.rnk-1)*cspace*TLCh.scale);
  335.     strcpy(TLCc.name,p);
  336.     nx=TLCc.x;ny=TLCc.y;
  337.     Tilt();
  338.     TLCc.x=nx;TLCc.y=ny;
  339.     writeC();
  340. }
  341.  
  342. void makeB(void)
  343. {
  344.     long z;
  345.  
  346.     //change box to polygon
  347.     TLCp.lyr=TLCb.lyr;
  348.     TLCp.w=0;
  349.     TLCp.nv=5;
  350.  
  351.     z=(long)((TLCb.lyr-1)*lspace*TLCh.scale);
  352.  
  353.     nx=TLCb.l;ny=TLCb.b;nz=z;
  354.     Tilt();
  355.     TLCp.x[0]=nx;TLCp.y[0]=ny;
  356.  
  357.     nx=TLCb.r;ny=TLCb.b;nz=z;
  358.     Tilt();
  359.     TLCp.x[1]=nx;TLCp.y[1]=ny;
  360.  
  361.     nx=TLCb.r;ny=TLCb.t;nz=z;
  362.     Tilt();
  363.     TLCp.x[2]=nx;TLCp.y[2]=ny;
  364.  
  365.     nx=TLCb.l;ny=TLCb.t;nz=z;
  366.     Tilt();
  367.     TLCp.x[3]=nx;TLCp.y[3]=ny;
  368.  
  369.     TLCp.x[4]=TLCp.x[0];
  370.     TLCp.y[4]=TLCp.y[0];
  371.     writeP();
  372. }
  373.  
  374. void makeT(void)
  375. {
  376.     //tilt text ref point
  377.     nz=(long)((TLCt.lyr-1)*lspace*TLCh.scale);
  378.  
  379.     nx=TLCt.x;ny=TLCt.y;
  380.     Tilt();
  381.     TLCt.x=nx;TLCt.y=ny;
  382.     writeT();
  383. }
  384.  
  385. void makeP(void)
  386. {
  387.     int p=0;
  388.     long z;
  389.  
  390.     z=(long)((TLCp.lyr-1)*lspace*TLCh.scale);
  391.  
  392.     if(TLCp.w!=0){
  393.         path2poly();
  394.         TLCp.w=0;
  395.     }
  396.     while(p<TLCp.nv){
  397.         nx=TLCp.x[p];ny=TLCp.y[p];nz=z;
  398.         Tilt();
  399.         TLCp.x[p]=nx;TLCp.y[p]=ny;
  400.         p++;
  401.     }
  402.     writeP();
  403. }
  404.  
  405. //TLC poly from TLC path
  406. void path2poly(void)
  407. {
  408.     int p,nv=0,first,ext;
  409.     long hw;
  410.  
  411.     //no poly allowed
  412.     if(TLCp.w==0) return;
  413.  
  414.     // extended end
  415.     if(TLCp.w>0) ext=0; else ext=1;
  416.  
  417.     // init expansion
  418.     npp=0;              //expand point number
  419.     first=1;            //first vertex
  420.     hw=labs(TLCp.w)/2;  //half path width
  421.  
  422.     p=0;
  423.     p1=TLCp.x[p];       //do first point
  424.     q1=TLCp.y[p++];
  425.  
  426.     //path expansion loop
  427.     while(p<TLCp.nv){
  428.         p2=TLCp.x[p];
  429.         q2=TLCp.y[p];
  430.  
  431.         if(p1==p2 && q1==q2)    //point1==point2
  432.             goto skip;
  433.  
  434.         if(p+1<TLCp.nv){        //look ahead
  435.             p3=TLCp.x[p+1];
  436.             q3=TLCp.y[p+1];
  437.             if(p2==p3 && q2==q3)    //point2==point3
  438.                 goto skip;
  439.         }
  440.         pathSeg(hw,first,p+1,ext);  //make segs
  441.         first=0;
  442.  
  443. skip:   //skip to next coord
  444.         p1=p2;
  445.         q1=q2;
  446.         p++;
  447.     }
  448.     TLCp.nv=makeTLCxy()+1;
  449. }
  450.  
  451.  
  452. //make a path segment
  453. int pathSeg(long w2,int first,int nv,int ext)
  454. {
  455.     static int k,aa;
  456.     double s,c;
  457.     long x1,x2,x3,x4;
  458.     long y1,y2,y3,y4;
  459.     double fx,fy,fcc,fss,f,fn,fh,fhc,fhs;
  460.     double fa,fta;
  461.  
  462. //  corners:
  463. //  ---2-----------------4---
  464. //     |                 |
  465. //    p1,q1             p2,q2
  466. //     |                 |
  467. //  ---1-----------------3---
  468.  
  469.  
  470. // make side lines
  471.  
  472.     // get trig
  473.     fx=p2-p1;
  474.     fy=q2-q1;
  475.  
  476.     fa=atan2(fy,fx);
  477.     fcc=cos(fa);
  478.     fss=sin(fa);
  479.     s=w2*fss;
  480.     c=w2*fcc;
  481.  
  482.     // first coordinate pair
  483.     if(first){
  484.         // segment beginning
  485.         aa=0;
  486.         x1=lround(p1+s);
  487.         y1=lround(q1-c);
  488.         x2=lround(p1-s);
  489.         y2=lround(q1+c);
  490.  
  491.         // extended end
  492.         if(ext){
  493.             x1=lround(x1-c);
  494.             y1=lround(y1-s);
  495.             x2=lround(x2-c);
  496.             y2=lround(y2-s);
  497.         }
  498.  
  499.         // save first points
  500.         px[npp]=x1;
  501.         py[npp]=y1;
  502.         npp++;
  503.         px[npp]=x2;
  504.         py[npp]=y2;
  505.     }
  506.     else{
  507.         if(aa){     // acute corner
  508.             x1=lround(p1+s-k*c);
  509.             y1=lround(q1-c-k*s);
  510.             x2=lround(p1-s+k*c);
  511.             y2=lround(q1+c+k*s);
  512.  
  513.             npp++;
  514.             px[npp]=x1; //extra vertex
  515.             py[npp]=y1;
  516.             npp++;
  517.             px[npp]=x2;
  518.             py[npp]=y2;
  519.             aa=0;
  520.         }
  521.         else{
  522.             //next coords from last coords
  523.             x1=x3;
  524.             x2=x4;
  525.             y1=y3;
  526.             y2=y4;
  527.         }
  528.     }
  529.  
  530.     // second coordinate pair
  531.  
  532.     //next corner coordinates
  533.     if(nv>=TLCp.nv){
  534.         // segment end
  535.         x3=lround(p2+s);
  536.         y3=lround(q2-c);
  537.         x4=lround(p2-s);
  538.         y4=lround(q2+c);
  539.  
  540.         if(ext){
  541.             // extended end
  542.             x3=lround(x3+c);
  543.             y3=lround(y3+s);
  544.             x4=lround(x4+c);
  545.             y4=lround(y4+s);
  546.         }
  547.     }
  548.     else{
  549.         // look at next segment
  550.         fta=fa;
  551.         fx=p3-p2;
  552.         fy=q3-q2;
  553.         fa=atan2(fy,fx);
  554.         f=(fa-fta)/2;
  555.         fn=cos(f);
  556.  
  557.         if(fn){     // not 180 deg bend
  558.             fh=w2*sin(f)/fn;
  559.             k=dsign(fh);
  560.         }
  561.         else{       // 180 deg bend
  562.             fh=2*w2;
  563.             k=0;
  564.         }
  565.         if(fabs(fh)>w2){    // acute bend
  566.             fh=k*w2;
  567.             aa=1;
  568.         }
  569.         // next coord pair
  570.         fhc=fcc*fh;
  571.         fhs=fss*fh;
  572.         x3=lround(p2+s+fhc);
  573.         y3=lround(q2-c+fhs);
  574.         x4=lround(p2-s-fhc);
  575.         y4=lround(q2+c-fhs);
  576.     }
  577.     npp++;
  578.     px[npp]=x3; // lower side
  579.     py[npp]=y3;
  580.     npp++;      // upper side
  581.     px[npp]=x4;
  582.     py[npp]=y4;
  583.  
  584.     return npp;
  585. }
  586.  
  587. int makeTLCxy(void)
  588. {
  589.     int q,n=0;
  590.  
  591.     if(npp<3) return -1;
  592.  
  593.     TLCp.x[0]=px[n];
  594.     TLCp.y[0]=py[n];
  595.     n++;
  596.     TLCp.x[1]=px[n];
  597.     TLCp.y[1]=py[n];
  598.  
  599.     // odd points
  600.     for(q=3;q<=npp;q+=2){
  601.         n++;
  602.         TLCp.x[n]=px[q];
  603.         TLCp.y[n]=py[q];
  604.     }
  605.     // even ponts
  606.     for(q=npp-1;q>=0;q-=2){
  607.         n++;
  608.         TLCp.x[n]=px[q];
  609.         TLCp.y[n]=py[q];
  610.     }
  611.     return n;
  612. }
  613.  
  614.  
  615. //************************
  616. //TLC readers and writers
  617. //************************
  618.  
  619. //read a cell record
  620. int readC(void)
  621. {
  622.     static char buf[TLCLIM];
  623.     char *s;
  624.  
  625.     buf[0]=0;
  626.     if(!feof(rTLCfile)){
  627.         fgets(buf,TLCLIM,rTLCfile);
  628.         strcpy(TLCc.name,chop(buf,9));
  629.  
  630.         fgets(buf,TLCLIM,rTLCfile);
  631.         chop(buf,36);
  632.  
  633.         if((s=strtok(buf," "))==NULL) return 0;
  634.         TLCc.o=atoi(s) & 0x07;
  635.  
  636.         if((s=strtok(NULL," "))==NULL) return 0;
  637.         TLCc.x=atol(s);
  638.  
  639.         if((s=strtok(NULL," "))==NULL) return 0;
  640.         TLCc.y=atol(s);
  641.         return 1;
  642.     }
  643.     return 0;
  644. }
  645.  
  646. //read a box record
  647. int readB(void)
  648. {
  649.     static char buf[TLCLIM];
  650.     char *s;
  651.  
  652.     buf[0]=0;
  653.     if(!feof(rTLCfile)){
  654.         fgets(buf,TLCLIM,rTLCfile);
  655.         chop(buf,TLCLIM);
  656.  
  657.         if((s=strtok(buf," "))==NULL) return 0;
  658.         TLCb.lyr=atoi(s);
  659.  
  660.         if((s=strtok(NULL," "))==NULL) return 0;
  661.         TLCb.l=atol(s);
  662.  
  663.         if((s=strtok(NULL," "))==NULL) return 0;
  664.         TLCb.b=atol(s);
  665.  
  666.         if((s=strtok(NULL," "))==NULL) return 0;
  667.         TLCb.r=atol(s);
  668.  
  669.         if((s=strtok(NULL," "))==NULL) return 0;
  670.         TLCb.t=atol(s);
  671.         return 1;
  672.     }
  673.     return 0;
  674. }
  675.  
  676. //read a path record
  677. int readP(void)
  678. {
  679.     static char buf[TLCLIM];
  680.     char *s;
  681.     int nv=0,p=0;
  682.  
  683.     buf[0]=0;
  684.     if(!feof(rTLCfile)){
  685.  
  686.         fgets(buf,TLCLIM,rTLCfile);
  687.         chop(buf,TLCLIM);
  688.  
  689.         if((s=strtok(buf," "))==NULL) return 0;
  690.         TLCp.lyr=atoi(s);
  691.  
  692.         if((s=strtok(NULL," "))==NULL) return 0;
  693.         TLCp.w=atol(s);
  694.  
  695.         if((s=strtok(NULL," "))==NULL) return 0;
  696.         nv=TLCp.nv=atoi(s);
  697.  
  698.         fgets(buf,TLCLIM,rTLCfile);
  699.         chop(buf,TLCLIM);
  700.  
  701.         if((s=strtok(buf," "))==NULL) return 0;
  702.         TLCp.x[p]=atol(s);  //1st X
  703.  
  704.         if((s=strtok(NULL," "))==NULL) return 0;
  705.         TLCp.y[p]=atol(s);  //1st Y
  706.         p++;
  707.         nv--;
  708.         while(nv>0){
  709.             if((s=strtok(NULL," "))==NULL){ //end of line read new line
  710.                 fgets(buf,TLCLIM,rTLCfile);
  711.                 chop(buf,TLCLIM);
  712.                 if((s=strtok(buf," "))==NULL) return 0;
  713.             }
  714.             TLCp.x[p]=atol(s);  //keep X
  715.  
  716.             if((s=strtok(NULL," "))==NULL) return 0;
  717.             TLCp.y[p]=atol(s);  //keep Y
  718.             p++;
  719.             nv--;
  720.         }
  721.         return 1;
  722.     }
  723.     return 0;
  724. }
  725.  
  726. //read a text record
  727. int readT(void)
  728. {
  729.     static char buf[TLCLIM];
  730.     char *s;
  731.  
  732.     buf[0]=0;
  733.     if(!feof(rTLCfile)){
  734.         fgets(buf,TLCLIM,rTLCfile);
  735.         chop(buf,TLCLIM);
  736.  
  737.         if((s=strtok(buf," "))==NULL) return 0;
  738.         TLCt.lyr=atoi(s);
  739.  
  740.         if((s=strtok(NULL," "))==NULL) return 0;
  741.         TLCt.siz=atol(s);
  742.  
  743.         if((s=strtok(NULL," "))==NULL) return 0;
  744.         TLCt.nv=atoi(s);
  745.  
  746.         if((s=strtok(NULL," "))==NULL) return 0;
  747.         TLCt.o=atoi(s);
  748.  
  749.         //coords
  750.         fgets(buf,TLCLIM,rTLCfile);
  751.         chop(buf,TLCLIM);
  752.  
  753.         if((s=strtok(buf," "))==NULL) return 0;
  754.         TLCt.x=atol(s);
  755.  
  756.         if((s=strtok(NULL," "))==NULL) return 0;
  757.         TLCt.y=atol(s);
  758.  
  759.         //text string
  760.         fgets(buf,TLCLIM,rTLCfile);
  761.         chop(buf,TLCLIM);
  762.         strcpy(TLCt.txt,buf);
  763.         return 1;
  764.     }
  765.     return 0;
  766. }
  767.  
  768. //write TLC header info
  769. void writeH(void)
  770. {
  771.     static char buf[10];
  772.  
  773.     fprintf(wTLCfile,"=H\n");
  774.     fprintf(wTLCfile,"%s\n",TLCh.name);
  775.     fprintf(wTLCfile,"%s\n",TLCh.lv);
  776.     fprintf(wTLCfile,"%s\n",TLCh.tv);
  777.     fprintf(wTLCfile,"%g\n",TLCh.scale);
  778.     fprintf(wTLCfile,"%s\n",TLCh.punit);
  779.     fprintf(wTLCfile,"%s\n",_strdate(buf));
  780.     fprintf(wTLCfile,"%s\n",_strtime(buf));
  781.     fprintf(wTLCfile,"%d %ld %ld %ld %ld\n",TLCh.rnk,TLCh.l,TLCh.b,TLCh.r,TLCh.t);
  782.     fprintf(wTLCfile,"%d %d %u %d\n",TLCh.bn,TLCh.pn,TLCh.vn,TLCh.cn);
  783. }
  784.  
  785. //write a cell record
  786. void writeC(void)
  787. {
  788.     TLCh.cn++;
  789.     fprintf(wTLCfile,"=C\n");
  790.     fprintf(wTLCfile,"%s\n",TLCc.name);
  791.     fprintf(wTLCfile,"%d %ld %ld %d\n",TLCc.o,TLCc.x,TLCc.y,0);
  792. }
  793.  
  794. //write a box record
  795. void writeB(void)
  796. {
  797.     TLCh.bn++;
  798.     fprintf(wTLCfile,"=B\n");
  799.     fprintf(wTLCfile,"%d %ld %ld %ld %ld\n",TLCb.lyr,TLCb.l,TLCb.b,TLCb.r,TLCb.t);
  800. }
  801.  
  802. //write a path record
  803. void writeP(void)
  804. {
  805.     unsigned n=0;   //coords start at 0
  806.  
  807.     TLCh.pn++;
  808.     TLCh.vn+=TLCp.nv;
  809.     fprintf(wTLCfile,"=P\n");
  810.     fprintf(wTLCfile,"%d %ld %d\n",TLCp.lyr,TLCp.w,TLCp.nv);
  811.  
  812.     //write 5 vertex/line
  813.     while(n<TLCp.nv){
  814.         fprintf(wTLCfile,"%ld %ld ",TLCp.x[n],TLCp.y[n]);
  815.         n++;
  816.         if(!(n % 5)) fprintf(wTLCfile,"\n");
  817.     }
  818.     if((n % 5)) fprintf(wTLCfile,"\n");
  819. }
  820.  
  821. //write a text record
  822. void writeT(void)
  823. {
  824.     TLCh.pn++;
  825.     TLCh.vn+=TLCt.nv;
  826.     fprintf(wTLCfile,"=T\n");
  827.     fprintf(wTLCfile,"%d %ld %u %d\n",TLCt.lyr,TLCt.siz,TLCt.nv,TLCt.o);
  828.     fprintf(wTLCfile,"%ld %ld\n",TLCt.x,TLCt.y);
  829.     fprintf(wTLCfile,"%s\n",TLCt.txt);
  830. }
  831.  
  832. //*****************
  833. //tilter functions
  834. //*****************
  835.  
  836. //tilt global vars nx,ny,nz
  837. void Tilt(void)
  838. {
  839.     if(axis1=='z') tiltZ(cos1,sin1);
  840.     if(axis1=='x') tiltX(cos1,sin1);
  841.     if(axis1=='y') tiltY(cos1,sin1);
  842.  
  843.     if(axis2=='z') tiltZ(cos2,sin2);
  844.     if(axis2=='x') tiltX(cos2,sin2);
  845.     if(axis2=='y') tiltY(cos2,sin2);
  846.  
  847.     if(axis3=='z') tiltZ(cos3,sin3);
  848.     if(axis3=='x') tiltX(cos3,sin3);
  849.     if(axis3=='y') tiltY(cos3,sin3);
  850. }
  851.  
  852. //tilt on Z axis
  853. void tiltZ(double c,double s)
  854. {
  855.     long xtmp,ytmp;
  856.  
  857.     xtmp=(long)(c*nx-s*ny);
  858.     ytmp=(long)(s*nx+c*ny);
  859.     nz=nz;
  860.     nx=xtmp;
  861.     ny=ytmp;
  862. }
  863.  
  864. //tilt on X axis
  865. void tiltX(double c,double s)
  866. {
  867.     long xtmp,ytmp;
  868.  
  869.     xtmp=nx;
  870.     ytmp=(long)(c*ny-s*nz);
  871.     nz=(long)(s*ny+c*nz);
  872.     nx=xtmp;
  873.     ny=ytmp;
  874. }
  875.  
  876. //tilt on Y axis
  877. void tiltY(double c,double s)
  878. {
  879.     long xtmp,ytmp;
  880.  
  881.     xtmp=(long)(c*nx+s*nz);
  882.     ytmp=ny;
  883.     nz=(long)(c*nz-s*nx);
  884.     nx=xtmp;
  885.     ny=ytmp;
  886. }
  887.  
  888. //****************
  889. //misc. functions
  890. //****************
  891.  
  892. //open any TLC for read
  893. int ropenTLC(char *name)
  894. {
  895.     static char buf[13];
  896.  
  897.     sprintf(buf,"%s.TLC",name);
  898.     if((rTLCfile=fopen(buf,"rt"))==NULL)
  899.         return 0;
  900.     return 1;
  901. }
  902.  
  903. //open any tilted TLC for write
  904. int wopenTLC(char *name)
  905. {
  906.     static char buf[13];
  907.  
  908.     sprintf(buf,"%s.TLC",tiltname(name));
  909.     if((wTLCfile=fopen(buf,"wt"))==NULL)
  910.         return 0;
  911.     return 1;
  912. }
  913.  
  914.  
  915. //round double to long
  916. long lround(double f)
  917. {
  918.     if(f>=0)
  919.         return (long)(f+.5);
  920.     else
  921.         return (long)(f-.5);
  922. }
  923.  
  924. char *chop(char *s,int c)
  925. {
  926.     int l;
  927.  
  928.     l=strlen(s);
  929.     if(s[l-1]=='\n')
  930.         s[l-1]='\0';        //remove '\n'
  931.     s[c]='\0';              //keep c chars
  932.     return s;               //return pointer
  933. }
  934.  
  935. char *tiltname(char *name)
  936. {
  937.     static char buf[10];
  938.  
  939.     strcpy(buf,"!");
  940.     if(strlen(name)>7)
  941.         strncpy(buf+1,name+1,7);
  942.     else
  943.         strncpy(buf+1,name,7);
  944.     buf[8]=0;
  945.  
  946.     return buf;
  947. }
  948.  
  949. int dsign(double n)
  950. {
  951.     if(n>0) return 1;
  952.     if(n==0) return 0;
  953.     if(n<0) return -1;
  954. }
  955.  
  956. //check for key abort
  957. int abrtCk(void)
  958. {
  959.     int key=0;
  960.  
  961.     if(kbhit()){
  962.         key=getch();
  963.         if(key==0x1B) return 1;
  964.         if(key==0 || key==0xE0) getch();
  965.         return 0;
  966.     }
  967.     return 0;
  968. }
  969.  
  970.  
  971. void HELP(void)
  972. {
  973. printf("This utility makes drawings that are useful for presentations. It works on\n");
  974. printf("TLC files in the current directory and rotates objects around any 3 axes\n");
  975. printf("(x,y,z) into a 3-D view. The X-axis is to the right, the Y-axis is upward\n");
  976. printf("and the Z-axis is out of the drawing plane. Rotations are all right-handed.\n");
  977. printf("This program works only on the named cells and not any lesser cells that a\n");
  978. printf("cell might contain. It only works correctly on lesser cells if they are not\n");
  979. printf("not rotated or flipped. Use the FLATTLC.EXE program to make a flat TLC file\n");
  980. printf("if the drawing has nested lesser cells\n");
  981. printf("\n");
  982. printf("The layers and cells are stacked by a constant spacing outward from the\n");
  983. printf("drawing plane. Layer spacing and cell spacing may be different.\n");
  984. printf("\n");
  985. printf("  Command Line:    axis=\"x\"or\"y\"or\"z\"  []=Optional\n");
  986. printf("\n");
  987. printf("  \"tilt angleaxis angleaxis angleaxis lspace cspace cellname [...cellname]\"\n");
  988. printf("\n");
  989. printf("Angles are in deg, lspace is the layer to layer spacing in physical units\n");
  990. printf("and cspace is the spacing of cells in physical units from the first layer.\n");
  991. printf("\n");
  992. printf("  Example:  \"tilt 45z -60x 0y 10 0 cellx\"   makes a nice oblique view.\n");
  993. printf("\n");
  994.  
  995.     printf("Press a key to Continue ...");
  996.     getch();
  997.     _clearscreen(_GCLEARSCREEN);
  998.  
  999. printf("\nContinued ...\n\n");
  1000. printf("On completion, new TLC files will be made that have the ! character added\n");
  1001. printf("to the beginning of their name. An original name should not exceed 7 \n");
  1002. printf("characters or the first character will be chopped.\n");
  1003. printf("\n");
  1004. printf("Any cell records found in a TLC file will have their cellnames changed as\n");
  1005. printf("above also, so that the tilted lesser cells will be correctly added.\n");
  1006. printf("\n");
  1007. printf("Composite 3-D views of cells containing other cells may be made by first\n");
  1008. printf("tilting the lesser cells, and then the main cell. TLC will then construct\n");
  1009. printf("the composite drawing if only the main cell is named.\n");
  1010. printf("\n");
  1011. printf("Layers and cells may be respaced for best clarity. Using fills and dashed\n");
  1012. printf("lines helps greatly to distinguish layers.\n");
  1013. printf("\n");
  1014. printf("The order of the rotations is important. Mathematically, the rotation\n");
  1015. printf("operations do not commute.\n");
  1016. printf("\n\n");
  1017. printf("Hint:  To visualize the rotations, take a rectangular unsymmetric object\n");
  1018. printf("  like a floppy disk and rotate it manually in front of you.\n");
  1019. printf("\n");
  1020.  
  1021.     printf("Press a key to Exit ...");
  1022.     getch();
  1023.     _clearscreen(_GCLEARSCREEN);
  1024. }
  1025.